<template>
  <div>
    <div>基本信息</div>
    <hr />
    <FormPanel ref="formpanel" :list="formList" :cols="4" labelWidth="90" :isSubmitBtn="false" @formChange="formchangeFun" />
    <br />

    <div>车辆信息</div>
    <hr />
    <FormPanel ref="formpanel1" :list="formList1" :cols="3" labelWidth="115" :isSubmitBtn="false" @formChange="formchangeFun" />
    <br />

    <div>描述信息</div>
    <hr />
    <FormPanel ref="formpanel2" :list="formList2" :cols="3" labelWidth="100" :isSubmitBtn="false" @formChange="formchangeFun" />
    <div v-show="shows">
      <hr />
      <p>现场分析问题列表</p>
      <br />
      <h4>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;温馨提示：请务必点击工具栏中查看现场分析问题列表按钮查看车辆信息，如果该车故障与列表中故障匹配，尽快联系需求人。</h4>
    </div>
    <div v-show="!shows">
      <p>诊断信息</p>
      <hr />
      <FormPanel ref="formpanel3" :list="formList3" :cols="4" labelWidth="100" :isSubmitBtn="false" @formChange="formchangeFun" />
      <br />
      <FilterTable
        ref="table"
        :columns="columns"
        :isColumnFilter="false"
        columnsRef="PartTable"
        :isToperInfo="false"
        :params="fetchParams"
        :datakey="'rows'"
        height="150"
        :onSyncTableData="changetablelist"
        :onRowSelectChange="columns => (this.selColumn = columns)"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <!-- :onEnterEvent="selectChangeHandle" -->
        <!-- :fetchapi="fetchApi" -->
      </FilterTable>
      <br />
      <FormPanel ref="formpanel4" :list="formList4" :cols="4" labelWidth="90" :isSubmitBtn="false" @formChange="formchangeFun" />
    </div>
    <BaseDialog :visible.sync="selectRepairOrderVisible" title="选择工单" destroyOnClose width="920px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <selectRepairOrder @close="closeRelated" />
    </BaseDialog>
    <!-- :rowData="formData"
        :selectArray="selectArray"
        @changedata="changedata"
    @close="closeLabour"-->
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button v-if="type !== 'show'" type="primary" @click="alterHandle">{{ btnText }}</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { searchFaultLocation, searchFaultType, queryLabour, addSave, editSave } from '@/api/repair/quaback/index';
import { findProjectModelList } from '@/api/repair/repair/labourManager'; // 车系
import selectRepairOrder from './selectRepairOrder';
export default {
  name: 'feedback',
  components: {
    selectRepairOrder
  },
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '新增', edit: '编辑', sure: '确定' };
    return {
      formList: this.createFrom(),
      formList1: this.createFrom1(),
      formList2: this.createFrom2(),
      formList3: this.createFrom3(),
      formList4: this.createFrom4(),
      columns: this.createTableColumns(),
      fetchParams: {},
      selectRepairOrderVisible: false,
      shows: this.rowData.show,
      repairOrderDate: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.getdictList();
    this.getModelList();
    this.init();
  },
  mounted() {},
  methods: {
    async init() {
      if (this.type === 'edit' || this.type === 'sure') {
        let res = await queryLabour(this.rowData.feedbackNo);
        console.log(this.rowData, res, 'this.rowData');
        //   if (this.type === 'edit')
        this.formList.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList1.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList.find(x => x.fieldName === 'billTypeCode').initialValue = +res.data.billTypeCode;
        this.formList1.find(x => x.fieldName === 'situationCode').initialValue = +res.data.situationCode;
        this.formList1.find(x => x.fieldName === 'marketImpactCode').initialValue = +res.data.marketImpactCode;
        this.formList2.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList2.find(x => x.fieldName === 'complainCode').initialValue = +this.rowData.complainCode;
        // this.formList2.find(x => x.fieldName === 'situationCode').initialValue = +this.rowData.situationCode;
      }
      if (this.type === 'sure') {
        this.formList.map(x => {
          x.disabled = true;
        });
        this.formList1.map(x => {
          x.disabled = true;
        });
      }
    },
    createFrom() {
      return [
        // {
        //   type: 'INPUT',
        //   label: '经销商',
        //   fieldName: 'costomerCode',
        //   disabled: true
        // },
        {
          type: 'INPUT',
          label: '反馈单号',
          fieldName: 'feedbackNo',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '单据类型',
          fieldName: 'billTypeCode',
          itemList: this.createDictList('8210')
          // disabled: true
        },
        {
          type: 'INPUT',
          label: '单据状态',
          fieldName: 'status',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '操作员',
          fieldName: 'operator',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '操作员电话',
          fieldName: 'operatorPhone',
          disabled: true
          // placeholder: '请输入...'
          // unitRender: props => {
          //   return <el-button icon="el-icon-search" onClick={() => this.selectBreakModal(props)}></el-button>;
          // }
        },
        {
          type: 'DATE',
          label: '操作日期',
          fieldName: 'operateDate'
          // rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '提交日期',
          fieldName: 'commitDate'
        }
      ];
    },
    createFrom1() {
      return [
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'roNo',
          unitRender: props => {
            return <el-button icon="el-icon-search" onClick={() => this.selectRepairOrder(props)}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: 'VIN码',
          fieldName: 'vin',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'vehiclePurpose',
          itemList: this.createDictList('8026'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'ownerName',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '行驶里程',
          fieldName: 'inMileage',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'series',
          disabled: true,
          itemList: []
          // change: this.getModelList
          // unitRender: props => {
          //   return <el-button icon="el-icon-search" onClick={() => this.selectBreakModal(props)}></el-button>;
          // }
        },
        {
          type: 'SELECT',
          label: '销售车型',
          fieldName: 'model',
          disabled: true,
          itemList: []
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'engineNo',
          disabled: true
        },
        {
          type: 'DATE',
          label: '生产日期',
          fieldName: 'product'
        },
        {
          type: 'DATE',
          label: '销售日期',
          fieldName: 'saleDate'
        },
        {
          type: 'CHECKBOX',
          label: '事故车标识',
          fieldName: 'accidentFlag',
          initialValue: 10041002,
          // selfCols: 0.4,
          // change: val => {},
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          }
        },
        {
          type: 'CHECKBOX',
          label: '非原厂件标识',
          fieldName: 'originalFlag',
          selfCols: 0.6,
          initialValue: 10041002,
          // change: val => {},
          options: {
            trueValue: 10041001,
            falseValue: 10041002
          }
        },
        {
          type: 'SELECT',
          label: '保养情况',
          fieldName: 'situationCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: this.createDictList('5157')
        },
        {
          type: 'INPUT',
          label: '保养缺失第几次',
          fieldName: 'maintenanceCount',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '市场影响度代码',
          fieldName: 'marketImpactCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: this.createDictList('9404')
        },
        {
          type: 'TEXT_AREA',
          label: '车辆加装附件情况',
          fieldName: 'addSituation',
          selfCols: 2,
          // rows: 4,
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        }
        // {
        //   type: 'DATE',
        //   label: '报损日期',
        //   disabled: true,
        //   fieldName: 'lossDate',
        //   valueFormat: 'YYYY-MM-DD'
        // },
        // {
        //   type: 'CHECKBOX',
        //   label: '是否售前维修',
        //   fieldName: 'isPreOrder',
        //   selfCols: 0.4,
        //   change: val => {},
        //   options: {
        //     trueValue: '10041001',
        //     falseValue: '10041002'
        //   }
        // },
      ];
    },
    createFrom2() {
      return [
        {
          type: 'SELECT',
          label: '抱怨来源',
          fieldName: 'complainCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: this.createDictList('5146')
        },
        {
          type: 'SELECT',
          label: '故障频率',
          fieldName: 'faultFrequencyCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: this.createDictList('9724')
        },
        {
          type: 'SELECT',
          label: '是否抛锚',
          fieldName: 'anchorFlag',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'DATE',
          label: '接车时间',
          fieldName: 'pickDate'
        },
        {
          type: 'SELECT',
          label: '故障部位',
          fieldName: 'faultPositionCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: []
        },
        {
          type: 'SELECT',
          label: '缺陷类型',
          fieldName: 'defectClassCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: []
        },
        {
          type: 'TEXT_AREA',
          label: '客户描述',
          selfCols: 1.5,
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          fieldName: 'customerDesc'
        },
        {
          type: 'TEXT_AREA',
          label: '故障描述',
          selfCols: 1.5,
          fieldName: 'faultDesc',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        }
      ];
    },
    createFrom3() {
      return [
        {
          type: 'SELECT',
          label: '故障材料',
          fieldName: 'complainCode',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }],
          itemList: []
        },
        {
          type: 'INPUT',
          label: '查看维修案例',
          fieldName: 'agreedTime'
          // rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '案例编号',
          fieldName: 'agreedTime'
          // rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '案例名称',
          fieldName: 'agreedTime'
          // rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '诊断分析',
          selfCols: 1.3,
          fieldName: 'agreedTime',
          rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '处理方案',
          selfCols: 1.3,
          fieldName: 'agreedTime'
          // rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '厂家技术反馈',
          selfCols: 1.3,
          fieldName: 'agreedTime'
          // rules: [{ required: true, message: '这是必填字段', trigger: 'blur' }]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          align: 'center',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '换下件代码',
          width: 150,
          showOverflowTooltip: true,
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true
        },
        {
          title: '换下件名称',
          dataIndex: 'dealerAbbreviation',
          sorter: true,
          filter: true
        },
        {
          title: '换上件代码',
          dataIndex: 'documentType',
          sorter: true,
          filter: true
        },
        {
          title: '换上件名称',
          dataIndex: 'feedbackNo',
          sorter: true,
          filter: true
        },
        {
          title: '主件',
          dataIndex: 'documentStatus',
          sorter: true,
          filter: true
          // hidden: true
        },
        {
          title: '数量',
          dataIndex: 'operationDate',
          showOverflowTooltip: true,
          sorter: true,
          filter: true
          // render: props => {
          //   const pyt = pinyin(props.row.labourName, { style: 4 })
          //     .join('')
          //     .toUpperCase();
          //   return <span>{pyt}</span>;
          // }
        }
      ];
    },
    createFrom4() {
      return [
        {
          type: 'INPUT',
          label: '处理人',
          fieldName: 'complainCode'
        },
        {
          type: 'DATE',
          label: '处理日期',
          fieldName: 'pickDate'
        },
        {
          type: 'INPUT',
          label: '处理说明',
          selfCols: 1.5,
          fieldName: 'complainCode'
        },
        {
          type: 'INPUT',
          label: '一审人',
          fieldName: 'complainCode'
        },
        {
          type: 'DATE',
          label: '一审日期',
          fieldName: 'pickDate'
        },
        {
          type: 'INPUT',
          label: '一审说明',
          selfCols: 1.5,
          fieldName: 'complainCode'
        },
        {
          type: 'INPUT',
          label: '二审人',
          fieldName: 'complainCode'
        },
        {
          type: 'DATE',
          label: '二审日期',
          fieldName: 'pickDate'
        },
        {
          type: 'INPUT',
          label: '二审说明',
          selfCols: 1.5,
          fieldName: 'complainCode'
        }
      ];
    },
    async getdictList() {
      let res = await searchFaultLocation(); // 故障部位
      this.formList2.find(item => item.fieldName === 'faultPositionCode').itemList = res.data.map(x => ({ text: x.faultLocationName, value: x.faultLocationCode }));
      let res1 = await searchFaultType(); // 缺陷类型
      this.formList2.find(item => item.fieldName === 'defectClassCode').itemList = res1.data.map(x => ({ text: x.defectTypeName, value: x.defectTypeCode }));
    },
    async getModelList() {
      let res2 = await findProjectModelList({ WXXMDY: 'WXXMDY' });
      this.formList1.find(item => item.fieldName === 'series').itemList = res2.data.map(x => ({ text: x.MODEL_LABOUR_NAME, value: x.SERIES_ID, index: x.index }));
    },
    // 保存/修改
    async alterHandle() {
      if (this.type === 'add') {
        let row = [];
        let rows = [];
        let obj = {};
        row[0] = this.$refs.formpanel.SUBMIT_FORM();
        row[1] = this.$refs.formpanel1.SUBMIT_FORM();
        row[2] = this.$refs.formpanel2.SUBMIT_FORM();
        obj = Object.assign(row[0], row[1], row[2]);
        obj['ownerNo'] = this.repairOrderDate.ownerNo;
        obj = JSON.parse(JSON.stringify(obj));
        let res = await addSave(obj);
        if (res.resultCode === 200) {
          this.$emit('changedata');
          this.$notify.success({ title: '提示信息', message: res.errMsg || '保存成功!' });
        }
      } else if (this.type === 'edit') {
        let row = [];
        let rows = [];
        let obj = {};
        row[0] = this.$refs.formpanel.SUBMIT_FORM();
        row[1] = this.$refs.formpanel1.SUBMIT_FORM();
        row[2] = this.$refs.formpanel2.SUBMIT_FORM();
        obj = Object.assign(row[0], row[1], row[2]);
        obj['ownerNo'] = this.repairOrderDate.ownerNo;
        obj['id'] = this.rowData.id;
        obj = JSON.parse(JSON.stringify(obj));
        let res = await editSave(obj);
        if (res.resultCode === 200) {
          this.$emit('changedata');
          this.$notify.success({ title: '提示信息', message: res.errMsg || '修改成功!' });
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false);
    },
    selectRepairOrder() {
      this.selectRepairOrderVisible = true;
    },
    async closeRelated(type, val) {
      this.repairOrderDate = val;
      if (type === 'submit') {
        for (let i in val) {
          val[i] = val[i] === null ? '' : val[i];
          for (let j = 0; j < this.formList1.length; j++) {
            if (i == this.formList1[j].fieldName && val[i]) {
              this.formList1[j].initialValue = val[i];
            }
          }
        }
      }
      this.formList1.find(x => x.fieldName === 'series').initialValue = +val.series;
      console.log(val, this.repairOrderDate, '拿来的数据');
      this.selectRepairOrderVisible = false;
      // await this.getModelList(this.repairOrderDate.series);
    },
    selectBreakModal() {
      // this.breakShowFlag = true;
    },
    changetablelist() {},
    formchangeFun() {}
  }
};
</script>
<style lang="less" scoped></style>
